//
//  SubCollectionCellFour.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou.com. All rights reserved.
//




#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT self.bounds.size.height

#import "SubCollectionCellFour.h"
#import "ModelForCollectionViewTwo.h"
#import "UIImageView+WebCache.h"

#import "NetworkHandlerBlock.h"
#import "DetailOfBookViewController.h"
#import "ModelForTypeOfDetail.h"
#import "ModelForTypeOfListOfDetail.h"
#import "ModelForListen.h"

@interface SubCollectionCellFour ()

@property (nonatomic, retain) UIView *view;

@property (nonatomic, retain) NSMutableArray *arrTempCoverPath;
@property (nonatomic, retain) NSMutableArray *arrTempTitle;


@property (nonatomic, retain) NSMutableArray *arrForShangYeCaiJing;
@property (nonatomic, retain) NSMutableArray *arrForITKeJi;
@property (nonatomic, retain) NSMutableArray *arrForJianKangYangSheng;
@property (nonatomic, retain) NSMutableArray *arrForXiaoYuan;
@property (nonatomic, retain) NSMutableArray *arrForQiChe;
@property (nonatomic, retain) NSMutableArray *arrForLvYou;

@end

@implementation SubCollectionCellFour

- (void)dealloc {

    [_view release];
    [_arrTempCoverPath release];
    [_arrTempTitle release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.arrForShangYeCaiJing = [NSMutableArray array];
        self.arrForITKeJi = [NSMutableArray array];
        self.arrForJianKangYangSheng = [NSMutableArray array];
        self.arrForXiaoYuan = [NSMutableArray array];
        self.arrForQiChe = [NSMutableArray array];
        self.arrForLvYou = [NSMutableArray array];
        
        [self createSubViews];
        [self handleData];
    }
    return self;
}


- (void)handleData {
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=8&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForShangYeCaiJing addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=18&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    //////////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForITKeJi addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=7&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForJianKangYangSheng addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=20&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    //////////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForXiaoYuan addObject:modelOfBook];
            }
        }
    }];
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=21&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForQiChe addObject:modelOfBook];
            }
        }
    }];
    
    
    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=22&contentType=album&device=android&scale=2&version=4.3.20.8" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        
        NSDictionary *tempDic = [result objectForKey:@"categoryContents"];
        NSArray *tempArr = [tempDic objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            if ([[dic allKeys] containsObject:@"tagName"]) {
                ModelForTypeOfDetail *modelOfBook = [[ModelForTypeOfDetail alloc]init];
                [modelOfBook setValuesForKeysWithDictionary:dic];
                
                NSArray *tempArr1 = [dic objectForKey:@"list"];
                for (NSDictionary *dic in tempArr1) {
                    ///////
                    ModelForListen *modelForDetail = [[ModelForListen alloc]init];
                    [modelForDetail setValuesForKeysWithDictionary:dic];
                    [modelOfBook.arrForListDetail addObject:modelForDetail];
                }
                [self.arrForLvYou addObject:modelOfBook];
            }
        }
    }];
    
}


- (void)createSubViews {
    
    self.view = [[UIView alloc]initWithFrame:self.bounds];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.view];
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            UIImageView *picture = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
             picture.frame = CGRectMake(WIDTH / 2 * j + 10,HEIGHT / 3  * i + 10,HEIGHT / 3 - 20,HEIGHT / 3 - 20);
            [self.view addSubview:picture];
            picture.tag = 10*i + j + 1;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(WIDTH / 2 * j, HEIGHT / 3 * i, WIDTH / 2 - 2, HEIGHT / 3 - 2);
            button.frame = CGRectMake(WIDTH / 2 * j, HEIGHT / 3 * i, WIDTH / 2 - 2, HEIGHT / 3 - 2);
            [button setTitleEdgeInsets:UIEdgeInsetsMake(10, HEIGHT / 3 , 10, 10)];
            [button setTitle:@"3D体验馆" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [self.view addSubview:button];
            button.tag = 100*i + j + 15;
        }
    }
}

- (void)passModelForCellFour:(NSArray *)arrForModel {
    if (arrForModel) {
        
    
    self.arrTempCoverPath = [NSMutableArray array];
    self.arrTempTitle = [NSMutableArray array];
    for (ModelForCollectionViewTwo *model in arrForModel) {
        [self.arrTempCoverPath addObject:model.coverPath];
        [self.arrTempTitle addObject:model.title];
    }
    
    // 此处赋值时注意button和picture的tag标记的重复.
    UIImageView *pic = [self.view viewWithTag:1];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[17]]];
    
    pic = [self.view viewWithTag:2];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[18]]];
    
    
    pic = [self.view viewWithTag:11];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[19]]];
    
    pic = [self.view viewWithTag:12];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[20]]];
    
    pic = [self.view viewWithTag:21];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[21]]];
    
    pic = [self.view viewWithTag:22];
    [pic sd_setImageWithURL:[NSURL URLWithString:self.arrTempCoverPath[22]]];
    
    UIButton *button = [self.view viewWithTag:15];
    [button setTitle:self.arrTempTitle[17] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:16];
    [button setTitle:self.arrTempTitle[18] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController1:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:115];
    [button setTitle:self.arrTempTitle[19] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController2:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:116];
    [button setTitle:self.arrTempTitle[20] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController3:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:215];
    [button setTitle:self.arrTempTitle[21] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController4:) forControlEvents:UIControlEventTouchUpInside];
    
    button = [self.view viewWithTag:216];
    [button setTitle:self.arrTempTitle[22] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
       [button addTarget:self action:@selector(enterToDetailOfBookViewController5:) forControlEvents:UIControlEventTouchUpInside];

}
}
- (void)enterToDetailOfBookViewController :(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForShangYeCaiJing;
    bookVC.title = @"商业财经";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController1:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForITKeJi;
    bookVC.title = @"IT科技";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController2:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForJianKangYangSheng;
    bookVC.title = @"健康养生";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController3:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForXiaoYuan;
    bookVC.title = @"校园";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController4:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForQiChe;
    bookVC.title = @"汽车";
    [vc.navigationController pushViewController:bookVC animated:NO];
}
- (void)enterToDetailOfBookViewController5:(UIButton *)button {
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    
    DetailOfBookViewController *bookVC = [[DetailOfBookViewController alloc]init];
    bookVC.arrForTypeOfDetail = self.arrForLvYou;
    bookVC.title = @"旅游";
    [vc.navigationController pushViewController:bookVC animated:NO];
}


@end
